package com.ssy.lingxi.product.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.product.api.model.request.PriceCurveSetRequest;
import com.ssy.lingxi.product.api.model.response.PriceCurveSetResponse;
import com.ssy.lingxi.product.entity.PriceCurveSet;
import com.ssy.lingxi.product.entity.commodity.Commodity;
import com.ssy.lingxi.product.repository.PriceCurveSetRepository;
import com.ssy.lingxi.product.repository.commodity.CommodityRepository;
import com.ssy.lingxi.product.service.IPriceCurveSetService;
import com.ssy.lingxi.scheduler.api.feign.ScheduleTaskFeign;
import com.ssy.lingxi.scheduler.api.model.PermanentTaskVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 价格曲线设置
 * @author jw.chen
 * @version 2.0.0
 * @date 2021/10/20
 */
@Service
public class PriceCurveSetServiceImpl implements IPriceCurveSetService {

    @Resource
    private CommodityRepository commodityRepository;

    @Resource
    private PriceCurveSetRepository priceCurveSetRepository;

    @Resource
    private ScheduleTaskFeign scheduleTaskFeign;

    /**
     * 查询价格曲线设置列表
     * @return 价格曲线设置列表
     */
    @Override
    public List<PriceCurveSetResponse> getPriceCurveSetList(UserLoginCacheDTO sysUser){
        List<PriceCurveSet> priceCurveSetList = priceCurveSetRepository.findByMemberIdAndMemberRoleId(sysUser.getMemberId(), sysUser.getMemberRoleId());
        return priceCurveSetList.stream().map(priceCurveSet -> BeanUtil.copyProperties(priceCurveSet, PriceCurveSetResponse.class)).collect(Collectors.toList());
    }

    /**
     * 新增价格曲线设置
     * @return 是否设置成功
     */
    @Transactional
    @Override
    public Boolean savePriceCurveSet(UserLoginCacheDTO sysUser, List<PriceCurveSetRequest> priceCurveSetRequestList){
        //删除旧数据
        List<PriceCurveSet> existPriceCurveSetList = priceCurveSetRepository.findByMemberIdAndMemberRoleId(sysUser.getMemberId(), sysUser.getMemberRoleId());
        if(existPriceCurveSetList != null && existPriceCurveSetList.size() > 0){
            priceCurveSetRepository.deleteInBatch(existPriceCurveSetList);
        }
        //新增
        List<PriceCurveSet> priceCurveSetList = priceCurveSetRequestList.stream().map(priceCurveSetRequest -> {
            PriceCurveSet priceCurveSet = new PriceCurveSet();
            priceCurveSet.setShopId(priceCurveSetRequest.getShopId());
            priceCurveSet.setStatus(priceCurveSetRequest.getStatus());
            priceCurveSet.setMemberId(sysUser.getMemberId());
            priceCurveSet.setMemberName(sysUser.getCompany());
            priceCurveSet.setMemberRoleId(sysUser.getMemberRoleId());
            priceCurveSet.setMemberRoleName(sysUser.getMemberRoleName());
            priceCurveSet.setCreateTime(System.currentTimeMillis());
            return priceCurveSet;
        }).collect(Collectors.toList());

        if(priceCurveSetList.size() > 0){
            //持久化数据
            priceCurveSetRepository.saveAll(priceCurveSetList);

            //只要有一个商城的商品需要统计，就创建定时任务
            boolean flag = priceCurveSetList.stream().anyMatch(PriceCurveSet::getStatus);
            if(flag){
                //创建定时任务
                PermanentTaskVO permanentTaskVO = new PermanentTaskVO();
                permanentTaskVO.setTaskName("product-priceCurveSet");
                permanentTaskVO.setServiceName("product-service");
                //每天晚上00:05分开始执行
                permanentTaskVO.setStartMinutes(5);
                permanentTaskVO.setCallbackUrl("/product/task/analyseCommodityPrice");
                //存在任务的话，不需要删除重建任务
                permanentTaskVO.setIsCreateNew(false);
                scheduleTaskFeign.createPermanentTask(permanentTaskVO);
            }
        }
        return true;
    }

    /**
     * 查询是否显示价格曲线
     * @return 是/否显示
     */
    @Override
    public Boolean getPriceCurveSet(Long shopId, Long commodityId) {
        Commodity commodity = commodityRepository.findById(commodityId).orElse(null);
        if(commodity != null){
            return priceCurveSetRepository.existsByMemberIdAndMemberRoleIdAndShopIdAndStatus(commodity.getMemberId(), commodity.getMemberRoleId(), shopId, true);
        }else{
            return false;
        }
    }

}

